package vn.egame.elockscreen.activity;

import vn.egame.elockscreen.ELockscreenPreferences;
import vn.egame.elockscreen.R;
import vn.egame.elockscreen.utils.Fonts;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("NewApi")
public class ConfirmPasscodeActivity extends Activity {

	private TextView btnCancel, btnDelete;
	private TextView tvMessage, tvState;
	private TextView btnPin0, btnPin1, btnPin2, btnPin3, btnPin4, btnPin5,
			btnPin6, btnPin7, btnPin8, btnPin9;
	private ImageView btnPinBlank1, btnPinBlank2, btnPinBlank3, btnPinBlank4;
	private ImageView[] btnPinBlank;
	private ImageView imgFailPin;
	private RelativeLayout layoutPin;
	private int positionInput;
	private ImageView mImgBg;

	private String correctPasscode;
	private StringBuilder inputPasscode;
	Animation animationFadeOut;
	private boolean isInputPin = true;
	private boolean isWait;

	private String statusConfirm;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.activity_set_pin);

		inputPasscode = new StringBuilder();
		animationFadeOut = AnimationUtils.loadAnimation(
				getApplicationContext(), R.anim.fade_out);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			statusConfirm = extras.getString("CONFIRM");

		}

		correctPasscode = ELockscreenPreferences.getInstance()
				.getValuePasscode();

		initView();

		if (statusConfirm.equals("DISABLE_PIN")) {

			Toast.makeText(getApplicationContext(),
					"Confirm disable Passcode!", Toast.LENGTH_SHORT).show();
			tvState.setText("Confirm disable Passcode");

		} else if (statusConfirm.equals("CHANGE_PIN")) {
			tvState.setText("Confirm change your Passcode");
			Toast.makeText(getApplicationContext(),
					"Confirm change your Passcode!", Toast.LENGTH_SHORT).show();

		}

	}

	public void initView() {

		layoutPin = (RelativeLayout) findViewById(R.id.layoutPin);

		mImgBg = (ImageView) findViewById(R.id.imgBg);

		WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
		Drawable wallpaperDrawable = wallpaperManager.getDrawable();
		Bitmap wallpaperBitmap = ((BitmapDrawable) wallpaperDrawable)
				.getBitmap();

		mImgBg.setImageBitmap(wallpaperBitmap);

		tvState = (TextView) findViewById(R.id.tvState);
		setFont(tvState);

		btnCancel = (TextView) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(buttonClickListener);
		setFont(btnCancel);

		tvMessage = (TextView) findViewById(R.id.tvMessage);
		setFont(tvMessage);

		imgFailPin = (ImageView) findViewById(R.id.imgFailPin);
		imgFailPin.setVisibility(View.GONE);

		btnDelete = (TextView) findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(buttonClickListener);
		setFont(btnDelete);

		btnPin0 = (TextView) findViewById(R.id.btnPin0);
		btnPin0.setOnClickListener(pinClickListener);
		setFont(btnPin0);

		btnPin1 = (TextView) findViewById(R.id.btnPin1);
		btnPin1.setOnClickListener(pinClickListener);
		setFont(btnPin1);

		btnPin2 = (TextView) findViewById(R.id.btnPin2);
		btnPin2.setOnClickListener(pinClickListener);
		setFont(btnPin2);

		btnPin3 = (TextView) findViewById(R.id.btnPin3);
		btnPin3.setOnClickListener(pinClickListener);
		setFont(btnPin3);

		btnPin4 = (TextView) findViewById(R.id.btnPin4);
		btnPin4.setOnClickListener(pinClickListener);
		setFont(btnPin4);

		btnPin5 = (TextView) findViewById(R.id.btnPin5);
		btnPin5.setOnClickListener(pinClickListener);
		setFont(btnPin5);

		btnPin6 = (TextView) findViewById(R.id.btnPin6);
		btnPin6.setOnClickListener(pinClickListener);
		setFont(btnPin6);

		btnPin7 = (TextView) findViewById(R.id.btnPin7);
		btnPin7.setOnClickListener(pinClickListener);
		setFont(btnPin7);

		btnPin8 = (TextView) findViewById(R.id.btnPin8);
		btnPin8.setOnClickListener(pinClickListener);
		setFont(btnPin8);

		btnPin9 = (TextView) findViewById(R.id.btnPin9);
		btnPin9.setOnClickListener(pinClickListener);
		setFont(btnPin9);

		btnPinBlank1 = (ImageView) findViewById(R.id.btnPinBlank1);
		btnPinBlank2 = (ImageView) findViewById(R.id.btnPinBlank2);
		btnPinBlank3 = (ImageView) findViewById(R.id.btnPinBlank3);
		btnPinBlank4 = (ImageView) findViewById(R.id.btnPinBlank4);

		btnPinBlank = new ImageView[] { btnPinBlank1, btnPinBlank2,
				btnPinBlank3, btnPinBlank4 };
	}

	private void setFont(TextView view) {
//		view.setTypeface(Fonts.getInstance().fontLight);
	}

	View.OnClickListener pinClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			if (!isWait) {
				if (v.getId() == R.id.btnPin0) {
					setInputPasscode(0);
				} else if (v.getId() == R.id.btnPin1) {
					setInputPasscode(1);
				} else if (v.getId() == R.id.btnPin2) {
					setInputPasscode(2);
				} else if (v.getId() == R.id.btnPin3) {
					setInputPasscode(3);
				} else if (v.getId() == R.id.btnPin4) {
					setInputPasscode(4);
				} else if (v.getId() == R.id.btnPin5) {
					setInputPasscode(5);
				} else if (v.getId() == R.id.btnPin6) {
					setInputPasscode(6);
				} else if (v.getId() == R.id.btnPin7) {
					setInputPasscode(7);
				} else if (v.getId() == R.id.btnPin8) {
					setInputPasscode(8);
				} else if (v.getId() == R.id.btnPin9) {
					setInputPasscode(9);
				}

				checkInput();
			}

		}
	};

	View.OnClickListener buttonClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.btnCancel) {

				if (statusConfirm.equals("DISABLE_PIN")) {

					Toast.makeText(getApplicationContext(),
							"Cancel disable Passcode!", Toast.LENGTH_SHORT)
							.show();

				} else if (statusConfirm.equals("CHANGE_PIN")) {
					Toast.makeText(getApplicationContext(),
							"Cancel change your Passcode!", Toast.LENGTH_SHORT)
							.show();

				}

				Log.d("Kaii", "Cancel");

				finish();

			} else if (v.getId() == R.id.btnDelete) {
				if (inputPasscode.length() > 0) {
					inputPasscode.deleteCharAt(inputPasscode.length() - 1);
					positionInput--;
					btnPinBlank[positionInput].setSelected(false);
				}

				Log.d("Kai", inputPasscode.toString());
			}
		}
	};

	private void setInputPasscode(int value) {
		if (inputPasscode.length() < 4) {
			inputPasscode.append(value);
			Log.d("Kai", inputPasscode.toString());
			btnPinBlank[positionInput].setSelected(true);
			positionInput++;
		}
	}

	Handler handler = new Handler();

	private void checkInput() {
		if (inputPasscode.length() == 4 && !isWait) {

			isWait = true;
			handler.postDelayed(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub

					if (checkPasscode()) {
						if (statusConfirm.equals("DISABLE_PIN")) {
							ELockscreenPreferences.getInstance()
									.setEnablePasscode(false);
							ELockscreenPreferences.getInstance()
									.setValuePasscode("");
							Toast.makeText(getApplicationContext(),
									"Disable Passcode success!", 1000).show();
							finish();
						} else if (statusConfirm.equals("CHANGE_PIN")) {
							Intent intent = new Intent(getApplicationContext(),
									SetPasscodeActivity.class);
							startActivity(intent);
							finish();
						}

					} else {
						Toast.makeText(getApplicationContext(),
								"Passcode invalid!!!", Toast.LENGTH_SHORT)
								.show();

						imgFailPin.setVisibility(View.VISIBLE);

						imgFailPin.startAnimation(animationFadeOut);
						animationFadeOut
								.setAnimationListener(new AnimationListener() {

									@Override
									public void onAnimationStart(
											Animation animation) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onAnimationRepeat(
											Animation animation) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onAnimationEnd(
											Animation animation) {
										// TODO Auto-generated method stub
										inputPasscode = new StringBuilder();
										for (int i = 0; i < 4; i++) {
											btnPinBlank[i].setSelected(false);
										}
										positionInput = 0;
										imgFailPin.setVisibility(View.GONE);
										isWait = false;
									}
								});

					}

				}
			}, 200);

		}
	}

	private boolean checkPasscode() {
		if (inputPasscode.length() == 4
				&& inputPasscode.toString().equals(correctPasscode.toString()))
			return true;
		return false;
	}

}
